package org.kev.transit.util;

import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.kev.transit.TransitConf;
import org.kev.transit.data.RouteType;
import org.kev.transit.data.TimeTable;
import org.kev.transit.data.Route;
import org.kev.transit.data.Direction;


/**
 * static methods that help deal with remote web services
 * they will connect with a remote web service passing it query parms and then
 * retun results that will be encoded using JSON text if successful.  Note this class make no
 * android calls so it can be run in plain Java.
 * @author kevin
 *
 */


public class WebHelper {
	private static final String TAG ="WebHelper";
	public final String STOP_URI="stop.php";
	public final String DIR_URI="direction.php";
	public final String ROUTE_TYPE_URI="routeType.php";
	public final String ROUTE_URI="route.php";
	public final String TIME_TABLE="timeTable.php";
	
	
	public static String getStop(String rType, String route,String dir) throws  HttpException,IOException{
		//Log.d(TAG,">>>getStop");
		String jsonReturn=null;
	     HttpClient client = new HttpClient();
	     String parms=
	    	 "?"+TimeTable.ROUTE+"="+route+"&"+
	    	 TimeTable.DIRECTION+"="+dir+"&"+
	    	 TimeTable.ROUTE_TYPE+"="+rType+"&";
	    // client.getHostConfiguration().setHost(TransitConf.url+"/stop.php");
         GetMethod getMethod = new GetMethod(TransitConf.url+"/stop.php"+parms);
        HttpMethodParams httpMethodParams = getMethod.getParams();
        httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        getMethod.setParams(httpMethodParams);
        client.executeMethod(getMethod);
        jsonReturn = getMethod.getResponseBodyAsString();
        System.out.println("jsonReturn="+jsonReturn);
        //Log.d(TAG,"<<< getStop");
		return jsonReturn;
	}
	public static String getRoute(String rType) throws HttpException, IOException{
		String jsonReturn=null;
	     HttpClient client = new HttpClient();
	     String parms=
	    	 "?"+Route.ROUTE_TYPE+"="+rType;
         GetMethod getMethod = new GetMethod(TransitConf.url+"/route.php"+parms);
        HttpMethodParams httpMethodParams = getMethod.getParams();
        httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        httpMethodParams.setParameter(RouteType.TABLE_NAME, "1");
        getMethod.setParams(httpMethodParams);
        client.executeMethod(getMethod);
        jsonReturn = getMethod.getResponseBodyAsString();
        System.out.println("jsonReturn="+jsonReturn);

		return jsonReturn;
	}
	public static String getRouteType() throws HttpException, IOException{
		String jsonReturn=null;
	     HttpClient client = new HttpClient();
	    // client.getHostConfiguration().setHost(TransitConf.url+"/routeType.php");

         GetMethod getMethod = new GetMethod(TransitConf.url+"/routeType.php");
        HttpMethodParams httpMethodParams = getMethod.getParams();
        httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        getMethod.setParams(httpMethodParams);
        client.executeMethod(getMethod);
        jsonReturn = getMethod.getResponseBodyAsString();
        System.out.println("jsonReturn="+jsonReturn);
		return jsonReturn;
	}
	public static String getDirection(String rtype,String route) throws HttpException, IOException{
		String jsonReturn=null;
	    HttpClient client = new HttpClient();
	     String parms=
	    	 "?"+Direction.ROUTE_TYPE+"="+rtype+"&"+Direction.ROUTE+"="+route;
	   	 

	    System.out.println("parms=");
        GetMethod getMethod = new GetMethod(TransitConf.url+"/direction.php"+parms);
	    System.out.println("getMethod="+getMethod.getQueryString());
        HttpMethodParams httpMethodParams = getMethod.getParams();
        httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
      //  httpMethodParams.setParameter(Route.TABLE_NAME, route);
      //  httpMethodParams.setParameter(RouteType.TABLE_NAME, rtype);
        getMethod.setParams(httpMethodParams);
        client.executeMethod(getMethod);
        jsonReturn = getMethod.getResponseBodyAsString();
        System.out.println("jsonReturn="+jsonReturn);

		return jsonReturn;
	}
	public static String getTimeTable(String rType, String route,String dir,String stopId) throws HttpException, IOException{
		//Log.d(TAG,">>> getTimeTable");
		String jsonReturn=null;
	     HttpClient client = new HttpClient();
	     String parms=
	    	 "?"+TimeTable.ROUTE+"="+route+"&"+
	    	 TimeTable.DIRECTION+"="+dir+"&"+
	    	 TimeTable.ROUTE_TYPE+"="+rType+"&"+
	    	 TimeTable.STOP_ID+"="+stopId;
	    GetMethod getMethod = new GetMethod(TransitConf.url+"/timeTable.php"+parms);
        HttpMethodParams httpMethodParams =getMethod.getParams();
        System.out.println("httpParms="+httpMethodParams);
        httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        getMethod.setParams(httpMethodParams);
      
        System.out.println("uri="+getMethod.getURI() );
        System.out.println("getMethod query="+getMethod.getQueryString());
        client.executeMethod(getMethod);
        jsonReturn = getMethod.getResponseBodyAsString();
        System.out.println("jsonReturn="+jsonReturn);
       // Log.d(TAG,"<<< getTimeTable");
		return jsonReturn;
	}

}
